#!/usr/bin/env python
# coding=utf-8

import os, sys, re, traceback, logging
import urllib
import time
import datetime
import getopt
import urllib2
import xml.sax
from xml.sax.handler import *
from Route import *



class XmlHandler(ContentHandler):

    """Crude extractor for quotations.dtd compliant XML document"""
    def __init__(self):
        self.in_supplier = 0
        self.in_title = 0
        self.in_rout_url = 0
        self.in_img_url = 0
        self.in_type = 0
        self.in_sights = 0
        self.in_outcity = 0
        self.in_days = 0
        self.in_dates = 0
        self.in_price = 0
        self.in_guide = 0
        self.in_go_t = 0
        self.in_back_t = 0
        self.in_detail = 0
        self.in_hotel = 0
        self.in_meal = 0
        self.in_traffic01 = 0
        self.in_traffic02 = 0
        self.in_tickets = 0
        self.in_payown = 0
        self.in_schedule = 0
        self.in_s_num = 0
        self.in_s_place = 0
        self.in_s_brief = 0
        self.in_s_brf = 0
        self.in_s_lunch = 0
        self.in_s_dinner = 0
        self.in_s_traffic = 0
        self.in_s_plcode = 0
        self.in_s_hotel = 0        
        self.in_expense = 0
        self.in_e_num=0
        self.in_e_name=0
        self.in_e_price=0
        self.in_e_place=0   
        self.in_shopping = 0
        self.in_sp_num=0
        self.in_sp_name=0
        self.in_sp_type=0
        self.in_sp_time=0
        self.in_sp_place=0
        self.level=0

        
    def get_route(self):
        return self._route

    def startElement(self, name, attrs):
    	if name == 'line':
            self.level+=1
            self._route=Route()
            self._route.site='22'
        elif name == 'agency':
            self.in_supplier = 1
        elif name == 'title':
            self.in_title = 1
        elif name == 'url':
            self.in_rout_url = 1
        elif name == 'lineimg':
            self.in_img_url = 1
        elif self.level==1 and name == 'type':
            self.in_type = 1
        elif name == 'characteristic':
            self.in_sights = 1
        elif name == 'startcity':
            self.in_outcity = 1
        elif name == 'duration':
			self.in_days = 1
        elif name == 'dates':
			self.in_dates = 1
        elif name == 'lowprice':
            self.in_price = 1
            self._route.price=''
        elif name == 'guideservice':
        	self.in_guide = 1
        elif name == 'deptraffic':
            self.in_go_t = 1
            self._route.go_t=''
        elif name == 'rettraffic':
            self.in_back_t = 1
            self._route.back_t=''
        elif name == 'remark':
            self.in_detail = 1
        elif name == 'hotel':
            self.in_hotel = 1
        elif name == 'meal':
        	self.in_meal = 1
        elif name == 'traffic01':
            self.in_traffic01 = 1
        elif name == 'traffic02':
            self.in_traffic02 = 1
        elif name == 'tickets':
            self.in_tickets = 1
        elif name == 'payown':
            self.in_payown = 1
        elif name == 'schedules':
            self.level=2
            self._route.schedules = []
        elif name == 'schedule':
            self.in_schedule = 1
            self._schedule = Schedule()
        elif self.in_schedule >0 and name == 'serialnumber':
            self.in_s_num = 1
        elif self.in_schedule >0 and name == 'place':
            self.in_s_place = 1
        elif self.in_schedule >0 and name == 'brief':
            self.in_s_brief = 1
        elif self.in_schedule >0 and name == 'breakfast':
            self.in_s_brf = 1
        elif self.in_schedule >0 and name == 'lunch':
            self.in_s_lunch = 1
        elif self.in_schedule >0 and name == 'dinner':
            self.in_s_dinner = 1
        elif self.in_schedule >0 and name == 'traffic':
            self.in_s_traffic = 1
        elif self.in_schedule >0 and name == 'planecode':
            self.in_s_plcode = 1
        elif self.in_schedule >0 and name == 'accommodation':
            self.in_s_hotel = 1
        elif name == 'expenses':
            self.level=2
            self._route.expenses = []
        elif name == 'expense':
            self.in_expense = 1
            self._expense = Expense()
        elif self.in_expense >0 and name == 'number':
            self.in_e_num = 1
        elif self.in_expense >0 and name == 'name':
            self.in_e_name = 1
        elif self.in_expense >0 and name == 'price':
            self.in_e_price = 1
        elif self.in_expense >0 and name == 'place':
            self.in_e_place = 1
        elif name == 'shops':
            self.level=2
            self._route.shops = []
        elif name == 'shopping':
            self.shopping = 1
            self._shop = Shop()
        elif self.in_shopping >0 and name == 'number':
            self.in_sp_num = 1
        elif self.in_shopping >0 and name == 'name':
            self.in_sp_name = 1
        elif self.in_shopping >0 and name == 'type':
            self.in_sp_type = 1
        elif self.in_shopping >0 and name == 'time':
            self.in_sp_time = 1
        elif self.in_shopping >0 and name == 'place':
            self.in_sp_place = 1

    def endElement(self, name):
        if name == 'line':
        	self.thisquote = ''
        elif name == 'agency':
            self.in_supplier = 0
        elif name == 'title':
            self.in_title = 0
        elif name == 'url':
            self.in_rout_url = 0
        elif name == 'lineimg':
            self.in_img_url = 0
        elif self.level==1 and name == 'type':
            self.in_type = 0
        elif name == 'characteristic':
            self.in_sights = 0
        elif name == 'startcity':
            self.in_outcity = 0
        elif name == 'duration':
            self.in_days = 0
        elif name == 'dates':
            self.in_dates = 0
        elif name == 'lowprice':
            self.in_price = 0
        elif name == 'guideservice':
            self.in_guide = 0
        elif name == 'deptraffic':
            self.in_go_t = 0
        elif name == 'rettraffic':
            self.in_back_t = 0
        elif name == 'remark':
            self.in_detail = 0
        elif name == 'hotel':
            self.in_hotel = 0
        elif name == 'meal':
            self.in_meal = 0
        elif name == 'traffic01':
            self.in_traffic01 = 0
        elif name == 'traffic02':
            self.in_traffic02 = 0
        elif name == 'tickets':
            self.in_tickets = 0
        elif name == 'payown':
            self.in_payown = 0        
        elif self.in_schedule >0 and name == 'serialnumber':
            self.in_s_num = 0
        elif self.in_schedule >0 and name == 'place':
            self.in_s_place = 0
        elif self.in_schedule >0 and name == 'brief':
            self.in_s_brief = 0
        elif self.in_schedule >0 and name == 'breakfast':
            self.in_s_brf = 0
        elif self.in_schedule >0 and name == 'lunch':
            self.in_s_lunch = 0
        elif self.in_schedule >0 and name == 'dinner':
            self.in_s_dinner = 0
        elif self.in_schedule >0 and name == 'traffic':
            self.in_s_traffic = 0
        elif self.in_schedule >0 and name == 'planecode':
            self.in_s_plcode = 0
        elif self.in_schedule >0 and name == 'accommodation':
            self.in_s_hotel = 0
        elif name == 'schedule':
            self.in_schedule = 0
            if len(self._schedule.s_num)>0:
                self._route.schedules.append(self._schedule)
        elif self.in_expense >0 and name == 'number':
            self.in_e_num = 0
        elif self.in_expense >0 and name == 'name':
            self.in_e_name = 0
        elif self.in_expense >0 and name == 'price':
            self.in_e_price = 0
        elif self.in_expense >0 and name == 'place':
            self.in_e_place = 0
        elif name == 'expense':
            self.in_expense = 0
            if len(self._expense.e_num)>0:
                self._route.expenses.append(self._expense)
        elif self.in_shopping >0 and name == 'number':
            self.in_sp_num = 0
        elif self.in_shopping >0 and name == 'name':
            self.in_sp_name = 0
        elif self.in_shopping >0 and name == 'type':
            self.in_sp_type = 0
        elif self.in_shopping >0 and name == 'time':
            self.in_sp_time = 0
        elif self.in_shopping >0 and name == 'place':
            self.in_sp_place = 0
        elif name == 'shopping':
            self.in_shopping = 0
            if len(self._shop.sp_num)>0:
                self._route.shops.append(self._shop)


    def characters(self, ch):
        if self.in_supplier:
            self._route.supplier = self._route.supplier + ch.strip().encode('utf-8')
        if self.in_title:
            self._route.title = self._route.title + ch.strip().encode('utf-8')
        if self.in_rout_url:
            self._route.rout_url = self._route.rout_url + ch.strip().encode('utf-8')
        if self.in_img_url:
            self._route.img_url = self._route.img_url + ch.strip().encode('utf-8')
        if self.in_type:
            self._route.type = self._route.type + ch.strip().encode('utf-8')
        if self.in_sights:
            self._route.sights = self._route.sights + ch.strip().encode('utf-8')
        if self.in_outcity:
            self._route.outcity = self._route.outcity + ch.strip().encode('utf-8')
        if self.in_days:
            self._route.days = str(self._route.days) + ch.strip().encode('utf-8')
        if self.in_dates:
            self._route.dates = self._route.dates + ch.strip().encode('utf-8')
        if self.in_price:
            self._route.price = str(self._route.price) + ch.strip().encode('utf-8')
        if self.in_guide:
            self._route.guide = self._route.guide + ch.strip().encode('utf-8')
        if self.in_go_t:
            self._route.go_t = self._route.go_t + ch.strip().encode('utf-8')
        if self.in_back_t:
            self._route.back_t = self._route.back_t + ch.strip().encode('utf-8')
        if self.in_detail:
            self._route.detail = self._route.detail + ch.strip().encode('utf-8')
        if self.in_hotel:
            self._route.hotel = self._route.hotel + ch.strip().encode('utf-8')
        if self.in_meal:
            self._route.meal = self._route.meal + ch.strip().encode('utf-8')
        if self.in_traffic01:
            self._route.traffic01 = self._route.traffic01 + ch.strip().encode('utf-8')
        if self.in_traffic02:
            self._route.traffic02 = self._route.traffic02 + ch.strip().encode('utf-8')
        if self.in_tickets:
            self._route.tickets = self._route.tickets + ch.strip().encode('utf-8')
        if self.in_payown:
            self._route.payown = self._route.payown + ch.strip().encode('utf-8')
        if self.in_schedule >0:
            '''构造schedule'''
            if self.in_s_num:
                self._schedule.s_num = self._schedule.s_num + ch.strip().encode('utf-8')
            if self.in_s_place:
                self._schedule.s_place = self._schedule.s_place + ch.strip().encode('utf-8')
            if self.in_s_brief:
                self._schedule.s_brief = self._schedule.s_brief + ch.strip().encode('utf-8')
            if self.in_s_brf:
                self._schedule.s_brf = self._schedule.s_brf + ch.strip().encode('utf-8')
            if self.in_s_lunch:
                self._schedule.s_lunch = self._schedule.s_lunch + ch.strip().encode('utf-8')
            if self.in_s_dinner:
                self._schedule.s_dinner = self._schedule.s_dinner + ch.strip().encode('utf-8')
            if self.in_s_traffic:
                self._schedule.s_traffic = self._schedule.s_traffic + ch.strip().encode('utf-8')
            if self.in_s_plcode:
                self._schedule.s_plcode = self._schedule.s_plcode + ch.strip().encode('utf-8')
            if self.in_s_hotel:
                self._schedule.s_hotel = self._schedule.s_hotel + ch.strip().encode('utf-8')
        if self.in_expense>0:
            if self.in_e_num:
                self._expense.e_num = self._expense.e_num + ch.strip().encode('utf-8')
            if self.in_e_name:
                self._expense.e_name = self._expense.e_name + ch.strip().encode('utf-8')
            if self.in_e_price:
                self._expense.e_price = self._expense.e_price + ch.strip().encode('utf-8')
            if self.in_e_place:
                self._expense.e_place = self._expense.e_place + ch.strip().encode('utf-8')
        if self.in_shopping>0:
            if self.in_sp_num:
                self._shop.sp_num = self._shop.sp_num + ch.strip().encode('utf-8')
            if self.in_sp_name:
                self._shop.sp_name = self._shop.sp_name + ch.strip().encode('utf-8')
            if self.in_sp_type:
                self._shop.sp_type = self._shop.sp_type + ch.strip().encode('utf-8')
            if self.in_sp_time:
                self._shop.sp_time = self._shop.sp_time + ch.strip().encode('utf-8')
            if self.in_sp_place:
                self._shop.sp_place = self._shop.sp_place + ch.strip().encode('utf-8')
        
                    
def do_crawl(route):
    try:
        i=0
        plane_str = ''
        dest_str = ''
        for sc in route.schedules:
            plane_str = plane_str + " #第" + str(i+1) + "天# "
            plane_str = plane_str + sc.s_brief
            dest_str = dest_str +',' +sc.s_brief.replace(',','-').replace('，','-')
            i+=1
        route.outcity=route.outcity.replace('市','')
        route.plane=plane_str
        dest_str=dest_str+' '
        route.dest=dest_str
        
        traffic=gettraffic(route.go_t)
        route.go_t=traffic.split('_')[0]
        route.back_t=traffic.split('_')[1]
        route.detail=route.sights
        '''更新线路信息到DB'''
        route.updRoute()
        route.updRoute_Schedules()
        route.updRoute_Shops()
        route.updRoute_Expense()
    except:
        print traceback.format_exc(sys.exc_info())

def gettraffic(traffic):
    t=''
    if traffic.find('往返'):
        t=getTrafficType(traffic.replace('往返',''))
        t=t+'_'+t
    elif traffic.find('去') and traffic.find('回'):
        t=getTrafficType(traffic.split('去')[0])
        t=t+'_'+getTrafficType(traffic.split('去')[1].replace('回',''))
    else:
        t='-_-'
    return t
        
        
def getTrafficType(t):
    if t.find('飞')>-1:
        t='飞机'
    elif t.find('火')>-1:
        t='火车'
    elif t.find('动')>-1:
        t='动车'
    elif t.find('卧')>-1:
        t='卧铺'
    elif t.find('船')>-1:
        t='邮轮'
    elif t.find('汽车')>-1:
        t='汽车'
    return t
        
    
routes_re = re.compile(r"<Product>([0-9]+)</Product>", re.X|re.S)


def usage():
	print '''Usage: %s [OPTIONS]...
Crawl hotel informations.

  -d,  --debug               enable pdb debugger

  -h,  --help                display this help and exit
''' % sys.argv[0]

xml_url='https://www.easytour.cn/xml/%s.xml?u=Easytour2010&p=EasytourPwd2010'

if __name__ == '__main__':
    script_path = os.path.dirname(os.path.realpath(__file__))
    os.chdir(script_path)
    do_debug = False

    try:
        opts, args = getopt.gnu_getopt(sys.argv[1:], 'hdno:p:', ['help', 'debug', 'no-cache', 'only-cache', 'provider'])
    except getopt.GetoptError:
        usage()
        sys.exit()

    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
            sys.exit()
        elif opt in ('-d', '--debug'):
            do_debug = True
        elif opt in ('-n', '--no-cache'):
            use_cache = False
        elif opt in ('-o', '--only-cache'):
            if arg.lower() in ('no', 'n', '0'):
                only_cache = False
            else:
                only_cache = True
        elif opt in ('-p', '--provider'):
            pass

    if do_debug:
        import pdb
        pdb.set_trace()
    
    try:
    	i=0
        routid=''
        html = urllib2.urlopen('https://www.easytour.cn/xml/productlist.xml?u=Easytour2010&p=EasytourPwd2010').read()
        route_list = routes_re.findall(html)
        for r in route_list:
            i+=1
            routid=r
            print str(i)+': '+xml_url.replace('%s', r)
            parser = xml.sax.make_parser()
            handler = XmlHandler()
            parser.setContentHandler(handler)
            try:
                parser.parse(xml_url.replace('%s', r))
                route=handler.get_route()                        
                do_crawl(handler.get_route())
            except:
                route=Route()
                route.rout_url='http://www.easytour.cn/Product/Content/'+routid
                route.deleteRoute()
                print 'delete line:'+route.rout_url
                #print traceback.format_exc(sys.exc_info())
            #if i>1:
            #    break
    except:
        print traceback.format_exc(sys.exc_info())



